<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/12/24
// +----------------------------------------------------------------------

namespace app\common\services\wechattemplate;

use app\common\facade\Arr;
use app\common\services\WechatTemplateService;
use think\Db;

/**
 *
 * Class UserTemplateService
 * @package app\common\services\wechattemplate
 * @author feraner<feraner@163.com>
 * @date 2019/12/24
 */
class UserTemplateService extends WechatTemplateService
{

    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 发送客服消息给用户（用户提交认证申请，如果长时间未审核，可以通过客服联系审核）
     * @param $userInfo
     *
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function approveKefu($postData)
    {
        $userInfo=DB::name("user")
                ->where('id',Arr::get($postData, 'user_id'))
                ->field("wechat_openid,id")
                ->find();
        $approveType = '提交成功通知';
        $this->clearSetData()
            ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
            ->set('template', 'user_approve_success')
            ->set('first', '尊敬的用户，恭喜你已经提交认证！')
            ->set('keyword1', $approveType)
            ->set('keyword2', '提交成功')
            ->set('keyword3', date('Y年m月d日 H时i分'))
            ->set('remark', '如需客服请联系微信(zshtc001)！')
            ->send();
    }



    /**
     * 用户认证成商家 -- 提醒代理商审核
     * @param $postData
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendApprovePerson($postData)
    {
        $agencyInfo = Db::name('system_agent_user')
            ->where('provinces_id', Arr::get($postData, 'provinceid'))
            ->where('city_id', Arr::get($postData, 'cityid'))
            ->where('pid', 0)
            ->field('id,bind_agent_user_id')
            ->find() ?: [];
        if ($agencyInfo) {
            $sendUserInfo = Db::name('user')
                ->where('id', Arr::get($agencyInfo, 'bind_agent_user_id'))
                ->find() ?: [];

            $this->clearSetData()
                ->set('template', 'user_approve_person')
                ->set('toUser', Arr::get($sendUserInfo, 'wechat_openid'))
                ->set('first', '您好，有新的用户申请认证成个人，请及时处理')
                ->set('keyword1', '实名认证成个人')
                ->set('keyword2', Arr::get($postData, 'true_name', ''))
                ->set('keyword3', date('Y-m-d H:i'))
                ->set('remark', '请及时联系用户核对身份，登录后台完成认证审核!')
                ->send();
        }

        return true;
    }


    /**
     * 用户申请认证成商家 -- 通知代理商审核信息
     * @param $postData
     *
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendApproveCompany($postData)
    {

        $agencyInfo = Db::name('system_agent_user')
            ->where('provinces_id', Arr::get($postData, 'provinceid'))
            ->where('city_id', Arr::get($postData, 'cityid'))
            ->where('county_id', Arr::get($postData, 'areaid'))
            ->where('pid', 0)
            ->field('id,bind_agent_user_id')
            ->find() ?: [];
        if ($agencyInfo) {
            $sendUserInfo = Db::name('user')
                ->where('id', Arr::get($agencyInfo, 'bind_agent_user_id'))
                ->find() ?: [];

            $this->clearSetData()
                ->set('toUser', Arr::get($sendUserInfo, 'wechat_openid', ''))
                ->set('template', 'user_approve_company')
                ->set('first', '您好，有新的用户申请认证成商家，请及时登录后台处理')
                ->set('keyword1', Arr::get($postData, 'company_name'))
                ->set('keyword2', Arr::get($postData, 'legal_person_name'))
                ->set('keyword3', Arr::get($postData, 'company_mobile'))
                ->set('keyword4', date('Y-m-d H:i'))
                ->set('remark', '请及时联系用户核对身份，登录后台完成认证审核!')
                ->send();
        }
    }




    /**
     * 认证成功消息通知
     * @param $userInfo
     *
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function approveSuccess($userInfo)
    {
        $approveType = '个人认证';
        if (Arr::valueEq($userInfo, 'type', 2)) {
            $approveType = '商家认证';
        }
        $this->clearSetData()
            ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
            ->set('template', 'user_approve_success')
            ->set('first', '尊敬的用户，恭喜你已经通过认证！')
            ->set('keyword1', $approveType)
            ->set('keyword2', '认证成功')
            ->set('keyword3', date('Y年m月d日 H时i分'))
            ->set('remark', '快去发布兼职吧，我们竭诚为您提供最优质的服务！')
            ->send();
    }




    /**
     * 认证失败消息通知
     * @param        $userInfo
     * @param string $message
     *
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function approveFalse($userInfo, $message = '')
    {
        $approveType = '个人认证';
        if (Arr::valueEq($userInfo, 'type', 2)) {
            $approveType = '商家认证';
        }
        $this->clearSetData()
            ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
            ->set('template', 'user_approve_false')
            ->set('first', '尊敬的用户，您申请的认证未通过审核!')
            ->set('keyword1', $approveType)
            ->set('keyword2', '认证失败')
            ->set('keyword3', date('Y年m月d日 H时i分'))
            ->set('keyword4', ($message ?: '资料不全,请重新提交认证'))
            ->set('remark', '请重新提交认证资料,我们竭诚为您提供最优质的服务!')
            ->send();

    }





}
